package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {


    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private  StrategyTagServiceImpl strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll() ;
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }
    //保存或者修改
    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //处理数据
        //目的地的处理
        Strategy strategy  =strategyService.get(strategyDetail.getStrategy().getId());
        Destination dest = strategy.getDest();//da攻略关联查询目的地
        strategyDetail.setDest(dest);
        //作者的处理，暂时不写

        //摘要的处理 1是由运营人员自己编写 2截取文章内容的前两百个字
        String content =strategyDetail.getStrategyContent().getContent();
        if(content.length()>200){
            strategyDetail.setSummary(content.substring(0,200));
        }else {
            strategyDetail.setSummary(content);
        }
        //是否国外

        List<Destination> toasts = destinationService.getToasts(dest.getId());//通过迭代查询查集合
        Destination country = toasts.get(0);//得到开始的国家是什么
        if (country.getId()==1){//如果默认是中国就是false
            strategyDetail.setIsabroad(false);
        }else {
            strategyDetail.setIsabroad(true);
        }


        //tags 标签的保存
        //先打破关系
        strategyDetailMapper.deleteRelation(strategyDetail.getId());
        if (StringUtils.hasLength(tags)){
            String[] strs = tags.split(",");
            for (String name:strs){
                StrategyTag t = new StrategyTag();
                t.setName(name);
                t.setState(StrategyTag.STATE_NORMAL);

                strategyTagService.saveOrUpdate(t);
                //维护关系
                strategyDetailMapper.insertRelation(strategyDetail.getId(),t.getId());
            }
        }



        if(strategyDetail.getId() ==null){
            //内容的保存
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);

            //内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());//设置id
            strategyContentMapper.insert(strategyDetail.getStrategyContent());
        }else{
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());//把id设置进去
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }
    }

    @Override
    public void changeState(int state, Long id) {
        strategyDetailMapper.updateByState(state,id);
    }

    @Override
    public List<StrategyDetail> getViewNumTop3(Long id) {

        return strategyDetailMapper.getViewNumTop3(id);
    }

    @Override
    public void updateReplynum(Long id, int replynum) {
        strategyDetailMapper.updateReplynum(id,replynum);
    }

    @Override
    public void updateStatisVO(StrategyStatisPersistenceVO vo) {
        strategyDetailMapper.updateStatisVO(vo);
    }

}
